package com.huhao.consumer.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
public class CombineService {

    private final WebClient client = WebClient.create();

    private final RestTemplate restTemplate = new RestTemplate();

    public Mono<String> callTestA() {
        return client.get()
                .uri("http://localhost:8081/test/a")
                .retrieve()
                .bodyToMono(String.class);
    }

    public Mono<String> callTestB() {
        return client.get()
                .uri("http://localhost:8081/test/b")
                .retrieve()
                .bodyToMono(String.class);
    }

    public Mono<String> callTestC() {
        return client.get()
                .uri("http://localhost:8081/test/c")
                .retrieve()
                .bodyToMono(String.class);
    }

    public Mono<String> combine() {
        return Mono
                .zip(callTestA(), callTestB(), callTestC())
                .map(tuple -> {
                    String rtn;
                    rtn = tuple.getT1() + "%" + tuple.getT2() + "%" + tuple.getT3();
                    return rtn;
                });
    }

    public String callTestAOld() {
        return restTemplate.getForObject("http://localhost:8081/test/a", String.class);
    }

    public String callTestBOld() {
        return restTemplate.getForObject("http://localhost:8081/test/b", String.class);
    }

    public String callTestCOld() {
        return restTemplate.getForObject("http://localhost:8081/test/c", String.class);
    }
}
